package edu.ustb.mis.dm.util;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import edu.ustb.mis.dm.Constants;
import edu.ustb.mis.dm.model.ClusterResult;
import edu.ustb.mis.dm.model.WClusterResult;

public class ResultAnalyzeUtils {
	private static void appendOutPutString(final StringBuffer sbf, final int i, final Map<String, Integer> idMap, final Map<String, Integer> summary) {
		sbf.append("第").append(i + 1).append("次(").append(idMap.size()).append("个):").append(idMap).append("\r\n");
		for (final String key : idMap.keySet()) {
			if (summary.containsKey(key)) {
				summary.put(key, summary.get(key) + 1);
			} else {
				summary.put(key, 1);
			}
		}
	}

	private static void appendOutPutString(final StringBuffer sbf, final Map<String, Integer> summary) {
		sbf.append("\r\n总体情况：").append(summary);
	}

	public static Map<String, Integer> bridgePointAnalyze(final List<ClusterResult> clusterResultList) {
		final Map<String, Integer> idMap = new HashMap<String, Integer>();
		for (final ClusterResult clusterResult : clusterResultList) {
			for (final String id : clusterResult.getClassIDList()) {
				if (idMap.containsKey(id)) {
					idMap.put(id, idMap.get(id) + 1);
				} else {
					idMap.put(id, 1);
				}
			}
		}
		final Iterator<Entry<String, Integer>> itr = idMap.entrySet().iterator();
		while (itr.hasNext()) {
			final Entry<String, Integer> entry = itr.next();
			if (entry.getValue() < 2) {
				itr.remove();
				idMap.remove(entry.getKey());
			}
		}
		return idMap;
	}

	public static List<Map<String, Integer>> bridgePointAnalyzeAll(final List<List<ClusterResult>> clusterResultMatrix) {
		final List<Map<String, Integer>> resultList = new ArrayList<Map<String, Integer>>();
		for (final List<ClusterResult> clusterResultList : clusterResultMatrix) {
			resultList.add(bridgePointAnalyze(clusterResultList));
		}
		return resultList;
	}

	public static void printBridgePointAnalyzeResult(final String path, final List<Map<String, Integer>> result) {
		final String fileName = new StringBuffer(path).append("-").append(result.size()).append("-analyze_result").append(Constants.TXT_SUFFIX).toString();
		try {
			final BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
			final StringBuffer sbf = new StringBuffer();
			final int size = result.size();
			Map<String, Integer> idMap;
			final Map<String, Integer> summary = new HashMap<String, Integer>();
			for (int i = 0; i < size; i++) {
				idMap = result.get(i);
				appendOutPutString(sbf, i, idMap, summary);
			}
			appendOutPutString(sbf, summary);
			bw.write(sbf.toString());
			bw.flush();
			bw.close();
		} catch (final IOException e) {
			e.printStackTrace();
		}
	}

	public static Map<String, Integer> wbridgePointAnalyze(final List<WClusterResult> clusterResultList) {
		final Map<String, Integer> idMap = new HashMap<String, Integer>();
		for (final WClusterResult clusterResult : clusterResultList) {
			for (final String id : clusterResult.getNameList()) {
				if (idMap.containsKey(id)) {
					idMap.put(id, idMap.get(id) + 1);
				} else {
					idMap.put(id, 1);
				}
			}
		}
		final Iterator<Entry<String, Integer>> itr = idMap.entrySet().iterator();
		while (itr.hasNext()) {
			final Entry<String, Integer> entry = itr.next();
			if (entry.getValue() < 2) {
				itr.remove();
				idMap.remove(entry.getKey());
			}
		}
		return idMap;
	}

	public static List<Map<String, Integer>> wbridgePointAnalyzeAll(final List<List<WClusterResult>> clusterResultMatrix) {
		final List<Map<String, Integer>> resultList = new ArrayList<Map<String, Integer>>();
		for (final List<WClusterResult> clusterResultList : clusterResultMatrix) {
			resultList.add(wbridgePointAnalyze(clusterResultList));
		}
		return resultList;
	}
}
